//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartPieGroupSmallerSlicesView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartPieGroupSmallerSlicesView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartPieGroupSmallerSlicesView

IMPLEMENT_DYNCREATE(CChartPieGroupSmallerSlicesView, CBCGPChartExampleView)

CChartPieGroupSmallerSlicesView::CChartPieGroupSmallerSlicesView()
	: CBCGPChartExampleView(CChartPieGroupSmallerSlicesView::IDD)
{
	//{{AFX_DATA_INIT(CChartPieGroupSmallerSlicesView)
	m_bGroupSlices = TRUE;
	m_bGroupSlicesInLegend = TRUE;
	m_nPerc = 5;
	m_strPercValue = _T("");
	m_bCustomLabel = FALSE;
	//}}AFX_DATA_INIT

	m_strPercValue.Format(_T("%d%%"), m_nPerc);
}

CChartPieGroupSmallerSlicesView::~CChartPieGroupSmallerSlicesView()
{
}

void CChartPieGroupSmallerSlicesView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartPieGroupSmallerSlicesView)
	DDX_Control(pDX, IDC_CUSTOM_LABEL, m_wndCustomLabel);
	DDX_Control(pDX, IDC_PERC, m_wndPerc);
	DDX_Control(pDX, IDC_GROUP_SMALLER_SLICES_IN_LEGEND, m_wndGroupSlicesInLegend);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_GROUP_SMALLER_SLICES, m_bGroupSlices);
	DDX_Check(pDX, IDC_GROUP_SMALLER_SLICES_IN_LEGEND, m_bGroupSlicesInLegend);
	DDX_Slider(pDX, IDC_PERC, m_nPerc);
	DDX_Text(pDX, IDC_PERC_VALUE, m_strPercValue);
	DDX_Check(pDX, IDC_CUSTOM_LABEL, m_bCustomLabel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartPieGroupSmallerSlicesView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartPieGroupSmallerSlicesView)
	ON_BN_CLICKED(IDC_GROUP_SMALLER_SLICES_IN_LEGEND, OnUpdateChart)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_GROUP_SMALLER_SLICES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CUSTOM_LABEL, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartPieGroupSmallerSlicesView diagnostics

#ifdef _DEBUG
void CChartPieGroupSmallerSlicesView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartPieGroupSmallerSlicesView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartPieGroupSmallerSlicesView message handlers

void CChartPieGroupSmallerSlicesView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_wndPerc.SetRange(1, 20, TRUE);
	m_wndPerc.SetPos(m_nPerc);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPInfoTipOptions infoTipOptions;
	infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_BottomLeft;
	
	m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);

	pChart->SetChartType(BCGPChartPie);

	pChart->AddChartData(_T("Apples"), 200);
	pChart->AddChartData(_T("Oranges"), 5);
	pChart->AddChartData(_T("Tomatoes"), 100);
	pChart->AddChartData(_T("Grapes"), 15);
	pChart->AddChartData(_T("Onions"), 4);
	pChart->AddChartData(_T("Melons"), 300);
	pChart->AddChartData(_T("Cucumbers"), 50);
	pChart->AddChartData(_T("Cherries"), 20);
	pChart->AddChartData(_T("Potatoes"), 150);
	pChart->AddChartData(_T("Cabbages"), 250);
	pChart->AddChartData(_T("Raspberries"), 8);
	pChart->AddChartData(_T("Strawberries"), 7);
	pChart->AddChartData(_T("Watermelons"), 3);

	pChart->ShowDataLabels();

	BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();

	dataLabelOptions.m_bUnderlineDataLabel = TRUE;
	dataLabelOptions.m_bDrawDataLabelBorder = FALSE;

	pChart->SetDataLabelsOptions(dataLabelOptions);

	m_bIsReady = TRUE;

	OnUpdateChart();
}

void CChartPieGroupSmallerSlicesView::OnUpdateChart() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		CString strLabel;

		if (m_bCustomLabel)
		{
			strLabel.Format(_T("Others: < %d%%"), m_nPerc);
		}

		pSeries->SetGroupSmallerSlices(m_bGroupSlices, (double)m_nPerc, m_bGroupSlicesInLegend, strLabel);
		pSeries->SetDefaultFillGradientType(CBCGPBrush::BCGP_GRADIENT_BEVEL);
	}

	m_wndGroupSlicesInLegend.EnableWindow(m_bGroupSlices);
	m_wndPerc.EnableWindow(m_bGroupSlices);
	m_wndCustomLabel.EnableWindow(m_bGroupSlices);

	pChart->Redraw();
}

void CChartPieGroupSmallerSlicesView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();

		m_strPercValue.Format(_T("%d%%"), m_nPerc);
		UpdateData(FALSE);
	}
}
